package com.zhaopin.gitlab;

import cn.hutool.core.comparator.CompareUtil;
import cn.hutool.core.util.ReUtil;
import com.zhaopin.utils.FileUtil;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.ObjectUtils;

import java.io.File;
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

/**
 * 目标：下线没用的data文件
 */
public class DownLoadDic {

    /**
     * 1、 寻找出所有的字典的 key名称
     * 2、 拿到所有的实验名称
     * 3、 最后查看挨个的目录是否有使用那些实验，有和没有分别记录到不同的文件中
     *
     * @param args
     */
    public static void main(String[] args) throws IOException {

        //1、首先找出所有的字典key名称
        List<String> dicList = new ArrayList<>();
        List<String> expTmpList = new ArrayList<>();
        String expFilePath = "/Users/zhaopin/IdeaProjects/aggregator/rpc_position-recommend/groovy/src/main/groovy/com/zhaopin/script/data/init";
        List<File> allFiles = FileUtil.getAllFiles(expFilePath);
        System.out.println("allFIle size : " + allFiles.size());
        int i = 0;
        for (File singleFile : allFiles) {

            List<String> list = FileUtils.readLines(singleFile, Charset.defaultCharset());
            for (String s : list) {
                if (s.startsWith("ConfigUtil.setData(")) {
                    i++;
                    expTmpList.add(s);
                }
            }
        }
        System.out.println("expTmpList:  " + expTmpList.size());
        for (String s : expTmpList) {
            String substring = s.substring(s.indexOf("\"") + 1, s.lastIndexOf("\""));
            dicList.add(substring);
        }

        //2、拿到所有的实验名称
        String expPath = "/Users/zhaopin/IdeaProjects/easyjar/dir/gitlab/project.txt";
        List<String> expPathList = FileUtils.readLines(new File(expPath), Charset.defaultCharset());


        Set<String> containsDicSet = new HashSet<>();
        String outPutPath = "/Users/zhaopin/IdeaProjects/easyjar/dir/downloaddic";
        ArrayList<String> outPutWriteContainsList = new ArrayList<>();
        String expXXPath = "/Users/zhaopin/IdeaProjects/project/%s/project/src/main/java";
        //查看核心代码
        String coreConfPath = "/Users/zhaopin/IdeaProjects/aggregator/rpc_position-recommend/groovy/src/main/groovy/com/zhaopin/script/config";
        String coreCorePath = "/Users/zhaopin/IdeaProjects/aggregator/rpc_position-recommend/groovy/src/main/groovy/com/zhaopin/script/library";

        //针对每个实验查看
        int j = 0;
        for (String dicName : dicList) {
            for (String s : expPathList) {
                List<File> allFiles1 = FileUtil.getAllFiles(String.format(expXXPath, s));
                for (File file : allFiles1) {
                    //下面就是看每个文件 是否包含字典了，然后考虑写入包含的文件还是不包含的文件
                    String fileContent = FileUtils.readFileToString(file, Charset.defaultCharset());
                    if (fileContent.contains(dicName)) {
                        containsDicSet.add(dicName);
                        //存储
                        outPutWriteContainsList.add(dicName + ":" + file.getAbsolutePath());

                    }
                }
            }

            List<File> coreConfPathList = FileUtil.getAllFiles(coreConfPath);
            for (File file : coreConfPathList) {
                String fileContent = FileUtils.readFileToString(file, Charset.defaultCharset());
                if (fileContent.contains(dicName)) {
                    containsDicSet.add(dicName);
                    //存储
                    outPutWriteContainsList.add(dicName + ":" + file.getAbsolutePath());

                }
            }

            List<File> coreCorePathList = FileUtil.getAllFiles(coreCorePath);
            for (File file : coreCorePathList) {
                String fileContent = FileUtils.readFileToString(file, Charset.defaultCharset());
                if (fileContent.contains(dicName)) {
                    containsDicSet.add(dicName);
                    //存储
                    outPutWriteContainsList.add(dicName + ":" + file.getAbsolutePath());

                }
            }
            j++;
            System.out.println(dicName + " : " + j);
        }


        List<String> cloneDicList = ObjectUtils.clone(dicList);

        List<String> containsDicSetList = containsDicSet.stream().sorted().collect(Collectors.toList());
        //使用到的字典
        for (String s : containsDicSetList) {
//            FileUtils.write(new File(outPutPath + "/containsDicList.txt" ),s,Charset.defaultCharset(),true);
//            FileUtils.write(new File(outPutPath + "/containsDicList.txt" ),"\r\n",Charset.defaultCharset(),true);
        }


        for (String s : dicList) {
            if(containsDicSet.contains(s)){
                cloneDicList.remove(s);
            }
        }

        //作为 positionSearch 集群处理的初始化字典
        List<String> cloneDicListSortedList = cloneDicList.stream().sorted().collect(Collectors.toList());
        //未使用到的字典
        for (String s : cloneDicListSortedList) {
//            FileUtils.write(new File(outPutPath + "/noneUseDicList.txt" ),s,Charset.defaultCharset(),true);
//            FileUtils.write(new File(outPutPath + "/noneUseDicList.txt" ),"\r\n",Charset.defaultCharset(),true);
        }


        System.out.println("-----------------------");
        List<String> outPutWriteContainsListList = outPutWriteContainsList.stream().sorted().collect(Collectors.toList());
        for (String s : outPutWriteContainsListList) {
//            FileUtils.write(new File(outPutPath + "/dicWithExpPath.txt" ),s,Charset.defaultCharset(),true);
//            FileUtils.write(new File(outPutPath + "/dicWithExpPath.txt" ),"\r\n",Charset.defaultCharset(),true);
        }


        //5、开始遍历 position集群的实验 以及遍历 position集群的core文件
        //查看核心代码
        String coreSearchConfPath = "/Users/zhaopin/IdeaProjects/aggregator/rpc_position-search/groovy/src/main/groovy/com/zhaopin/script/config";
        String coreSearchCorePath = "/Users/zhaopin/IdeaProjects/aggregator/rpc_position-search/groovy/src/main/groovy/com/zhaopin/script/library";
        String coreRpcConfPath = "/Users/zhaopin/IdeaProjects/java-s-aggregator/groovy/src/main/groovy/com/zhaopin/script/config";
        String coreRpcCorePath = "/Users/zhaopin/IdeaProjects/java-s-aggregator/groovy/src/main/groovy/com/zhaopin/script/library";


        String positionExpPath = "/Users/zhaopin/IdeaProjects/easyjar/dir/gitlab/position.txt";
        List<String> positionExpPathList = FileUtils.readLines(new File(positionExpPath), Charset.defaultCharset());
        String expPositionPath = "/Users/zhaopin/IdeaProjects/position/%s/project/src/main/java";

        HashSet<Object> needRemoveSet = new HashSet<>();

        int m = 0;
        for (String dicName : cloneDicListSortedList) {
            for (String s : positionExpPathList) {
                List<File> allFiles1 = FileUtil.getAllFiles(String.format(expPositionPath, s));
                for (File file : allFiles1) {
                    //下面就是看每个文件 是否包含字典了，然后考虑写入包含的文件还是不包含的文件
                    String fileContent = FileUtils.readFileToString(file, Charset.defaultCharset());
                    if (fileContent.contains(dicName)) {
                        needRemoveSet.add(dicName);
                    }
                }
            }

            List<File> coreConfPathList = FileUtil.getAllFiles(coreSearchConfPath);
            for (File file : coreConfPathList) {
                String fileContent = FileUtils.readFileToString(file, Charset.defaultCharset());
                if (fileContent.contains(dicName)) {
                    needRemoveSet.add(dicName);


                }
            }

            List<File> coreCorePathList = FileUtil.getAllFiles(coreSearchCorePath);
            for (File file : coreCorePathList) {
                String fileContent = FileUtils.readFileToString(file, Charset.defaultCharset());
                if (fileContent.contains(dicName)) {
                    needRemoveSet.add(dicName);

                }
            }
            List<File> coreRpcConfPathList = FileUtil.getAllFiles(coreRpcConfPath);
            for (File file : coreRpcConfPathList) {
                String fileContent = FileUtils.readFileToString(file, Charset.defaultCharset());
                if (fileContent.contains(dicName)) {
                    needRemoveSet.add(dicName);


                }
            }

            List<File> coreRpcCorePathList = FileUtil.getAllFiles(coreRpcCorePath);
            for (File file : coreRpcCorePathList) {
                String fileContent = FileUtils.readFileToString(file, Charset.defaultCharset());
                if (fileContent.contains(dicName)) {
                    needRemoveSet.add(dicName);

                }
            }

            m++;
            System.out.println(dicName + " : " + m);
        }
        //cloneDicListSortedList 初始字典
        //needRemoveSet 要移除的 字典
        List<String> cloneDicListSortedListClone = ObjectUtils.clone(cloneDicListSortedList);
        for (String s : cloneDicListSortedList) {
            if (needRemoveSet.contains(s)) {
                cloneDicListSortedListClone.remove(s);
            }
        }





        System.out.println("--------------------------------------------------");
        for (Object o : needRemoveSet) {
            System.out.println(o);
        }
        System.out.println("--------------------------------------------------");

        for (String s : cloneDicListSortedListClone) {
            System.out.println(s);
        }
    }




}
